(function() {
	/** *************** SRSRotationSystem *********************** */
	function SRSRotationSystem() {
		this.rotationMap = {};
		this.initPieceRotationMatrix();
	}

	SRSRotationSystem.prototype.calculateRotationIndex = function(pieceType, rotation) {
		var rotations = this.rotationMap[pieceType];
		var rotationNum = Math.abs(rotation) % rotations.length;

		if (rotation < 0) {
			rotationNum = rotations.length - rotationNum;
		}

		return rotationNum;
	};

	SRSRotationSystem.prototype.getPieceMatrix = function(pieceType, rotation) {
		return this.rotationMap[pieceType][this.calculateRotationIndex(pieceType, rotation)].clone();
	};

	SRSRotationSystem.prototype.initPieceRotationMatrix = function() {
		this.rotationMap = {};
		this.initIPieceRotationMatrix();
		this.initJPieceRotationMatrix();
		this.initLPieceRotationMatrix();
		this.initOPieceRotationMatrix();
		this.initSPieceRotationMatrix();
		this.initTPieceRotationMatrix();
		this.initZPieceRotationMatrix();
	};

	SRSRotationSystem.prototype.initIPieceRotationMatrix = function() {
		var pieceMatrix = [ //
		0, 0, 0, 0, //
		1, 1, 1, 1, //
		0, 0, 0, 0, //
		0, 0, 0, 0 ]; //

		var matrix = new Matrix(4, pieceMatrix);

		this.rotationMap[PieceType.I] = [ //
		matrix.clone(), //
		matrix.clone().rotate(+1), //
		matrix.clone().rotate(+2), //
		matrix.clone().rotate(+3) //
		];//
	};

	SRSRotationSystem.prototype.initJPieceRotationMatrix = function() {
		var pieceMatrix = [ //
		1, 0, 0, //
		1, 1, 1, //
		0, 0, 0 ];//

		var matrix = new Matrix(3, pieceMatrix);

		this.rotationMap[PieceType.J] = [ //
		matrix, //
		matrix.clone().rotate(+1),//
		matrix.clone().rotate(+2), //
		matrix.clone().rotate(+3) //
		];//
	};

	SRSRotationSystem.prototype.initLPieceRotationMatrix = function() {
		var pieceMatrix = [ //
		0, 0, 1, //
		1, 1, 1, //
		0, 0, 0 ]; //

		var matrix = new Matrix(3, pieceMatrix);

		this.rotationMap[PieceType.L] = [ //
		matrix, //
		matrix.clone().rotate(+1), //
		matrix.clone().rotate(+2), //
		matrix.clone().rotate(+3) //
		];//
	};

	SRSRotationSystem.prototype.initOPieceRotationMatrix = function() {
		var pieceMatrix = [//
		1, 1, //
		1, 1 ];//

		var matrix = new Matrix(2, pieceMatrix);

		this.rotationMap[PieceType.O] = [ //
		matrix, //
		matrix,//
		matrix, //
		matrix //
		];//
	};

	SRSRotationSystem.prototype.initSPieceRotationMatrix = function() {
		var pieceMatrix = [ //
		0, 1, 1, //
		1, 1, 0, //
		0, 0, 0 ];//

		var matrix = new Matrix(3, pieceMatrix);

		this.rotationMap[PieceType.S] = [ //
		matrix, //
		matrix.clone().rotate(+1),//
		matrix.clone().rotate(+2), //
		matrix.clone().rotate(+3) //
		];//
	};

	SRSRotationSystem.prototype.initTPieceRotationMatrix = function() {
		var pieceMatrix = //
		[ 0, 1, 0, //
		1, 1, 1, //
		0, 0, 0 ];//

		var matrix = new Matrix(3, pieceMatrix);

		this.rotationMap[PieceType.T] = [ //
		matrix, //
		matrix.clone().rotate(+1),//
		matrix.clone().rotate(+1), //
		matrix.clone().rotate(+1) //
		];//
	};

	SRSRotationSystem.prototype.initZPieceRotationMatrix = function() {
		var pieceMatrix = //
		[ 1, 1, 0, //
		0, 1, 1, //
		0, 0, 0 ];//

		var matrix = new Matrix(3, pieceMatrix);

		this.rotationMap[PieceType.Z] = [ //
		matrix, //
		matrix.clone().rotate(+1),//
		matrix.clone().rotate(+1), //
		matrix.clone().rotate(+1) //
		];//
	};

	var testPkg = allocatePackage("it.koutifaris._tetris");
	testPkg.SRSRotationSystem = SRSRotationSystem;
}());
